﻿using System;
using System.Windows;
using TFSChangeLog.View.Services;
using TFSChangeLog.ViewModel;

// TODO: Output files should be saved in AppData\TFSChangeLog folder.
// TODO: Allow user to select Labels instead of Changesets.
// TODO: Select Root Branch on loading branch information from TFS.
// TODO: Resizing window should adjust all the data on the screen.
// TODO: Write documentation comments and remove unnecesssary blocks of commented code.
// TODO: Report Generated time should show TFS Server Local time or UTC.


namespace TFSChangeLog
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        #region Event Handlers
        /// <summary>
        /// Subscribes to View Model events when data context is set.
        /// </summary>
        /// <remarks>
        /// This method subscribes to the view model's SelectProjectWorkStarted and
        /// SelectProjectWorkEnded events. This allows users to control display of the Progressbar
        /// provided by the application.
        /// </remarks>
        private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var viewModel = (MainWindowViewModel)e.NewValue;
            viewModel.SelectProjectWorkStarted += this.OnWorkStarting;
            viewModel.SelectProjectWorkEnded += this.OnWorkEnding;

            viewModel.SelectProjectBranchNodeWorkStarted += this.OnWorkStarting;
            viewModel.SelectProjectBranchNodeWorkEnded += this.OnWorkEnding;

            viewModel.SelectGenerateReleaseNotesWorkstarted += this.OnWorkStarting;
            viewModel.SelectGenerateReleaseNotesWorkEnded += this.OnWorkEnding;

            viewModel.SelectGenerateXmlChnagesWorkStarted += this.OnWorkStarting;
            viewModel.SelectGenerateXmlChnagesWorkEnded += this.OnWorkEnding;

            viewModel.ChangeSetSelectionError += this.onChangeSetSelectionError;
            viewModel.EmptyFileExtensionError += this.onEmptyFileExtensionError;

        }
        private void onChangeSetSelectionError(object sender, EventArgs e)
        {
            MessageBox.Show("Selected Value in \"Start From ChangeSet\"  (" + this.cmbStartFromChangeSet.SelectedValue + 
                ") should be less then \"End To ChangeSet\" (" +  this.cmbEndToChangeSet.SelectedValue + ").",
                "ChangeSet Selection Error",
               MessageBoxButton.OK,MessageBoxImage.Error,MessageBoxResult.OK);
        }

        /// <summary>Summary description for <c>MainWindow</c> method.</summary>
        /// <param name="sender">TODO: Summary description for <c>sender</c> parameter of type <c>object</c>.</param>
        /// <param name="e">TODO: Summary description for <c>e</c> parameter of type <c>System.EventArgs</c>.</param>
        private void onEmptyFileExtensionError(object sender, EventArgs e)
        {
            MessageBox.Show(
                            "File extensions cannot be empty for File changes",
                            "File extensions Error",
                            MessageBoxButton.OK,
                            MessageBoxImage.Error,
                            MessageBoxResult.OK);
        }


        /// <summary>
        /// Displays the Progress dialog.
        /// </summary>
        private void OnWorkStarting(object sender, EventArgs e)
        {
            var mainWindowViewModel = (MainWindowViewModel)this.DataContext;
            var progressDialogViewModel = mainWindowViewModel.progressDialogViewModel;
            ViewServices.ShowProgressDialog(this, progressDialogViewModel);
        }

        /// <summary>
        /// Closes the Progress dialog.
        /// </summary>
        private void OnWorkEnding(object sender, EventArgs e)
        {
            ViewServices.CloseProgressDialog();
        }
        #endregion
        
    }
}
